package ooo.reindeer.springboot.starter.cedf;

import ooo.reindeer.cedf.components.IComponent;
import ooo.reindeer.cedf.components.IComponentsLoader;
import ooo.reindeer.commons.Property;
import ooo.reindeer.logging.ILogger;
import ooo.reindeer.logging.LogUtil;

public class ComponentsLoader implements IComponentsLoader {
    private static final ILogger logger = LogUtil.getLogger(ComponentsLoader.class);

    @Override
    public <T extends IComponent> T newInstance(String id, String className, Property config) {
        try {
            logger.info("newInstance:name=[{}]	config=[{}]", className, config);
            Class<IComponent> clazz = (Class<IComponent>)Class.forName(className);
            IComponent component = SpringContext.createBean(clazz);
            component.init(id,config);
            return (T)component;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
